home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11978 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  85 lines

  1. Newsgroups: comp.lang.c
  2. Path: cdf.toronto.edu!news
  3. From: Laventure Derek <a228lave@cdf.toronto.edu>
  4. Subject: Re: Remove/Insert Function
  5. Content-Type: text/plain; charset=us-ascii
  6. Message-ID: <3159B67C.29D6@cdf.toronto.edu>
  7. Sender: news@cdf.toronto.edu (Usenet News)
  8. Nntp-Posting-Host: mstlim
  9. Content-Transfer-Encoding: 7bit
  10. Organization: Computing Disciplines Facility, University of Toronto
  11. References: <4j9m80$1cm@freenet-news.carleton.ca>
  12. Mime-Version: 1.0
  13. Date: Wed, 27 Mar 1996 21:43:24 GMT
  14. X-Mailer: Mozilla 2.0 (X11; I; SunOS 5.3 sun4c)
  15.  
  16. Hi!  
  17.  
  18. Jerry Boyd wrote:
  19. > Anyone have suggestions as to how I would WRITE a
  20. > function (called remove_entry) where the argument
  21. > would be a pointer into a list.  The function
  22. > would remove the entry after the one that is
  23. > pointed to by the argument.
  24.  
  25. Well, I'm not sure how lost you are here, but I'll answer on an
  26. algorithm level.  First, my advice re: anything pointer related:
  27. pictures.  I find that w/o pictures, I can't do much.  So, here's what
  28. you have:
  29.  
  30.   ---------    ---------    ---------    ---------
  31.   |    | .|--> |    | .|--> |    | .|--> |    | .|-->etc...
  32.   ---------    ---------    ---------    ---------
  33.                  ^             ^Node to be deleted...
  34.                  |Node pointed to by argument (*current)
  35.  
  36. I'll use a basic node type as follows, here.
  37.  
  38. struct Node{
  39.    int num;
  40.    Node *next;
  41. } Node;
  42.  
  43. I'll call the argument pointer current.
  44.  
  45. So, what needs to be done is:
  46.  
  47. 1)  Save a temp pointer to the node to be deleted (next pointer of the
  48. node pointed to by current).
  49.  
  50. 2)  Set the next pointer of the node pointed to by current to be the
  51. next pointer of the node pointed to by temp.
  52.  
  53. So, the picture changes to the following:
  54.  
  55.                          -----------------\
  56.   ---------    --------- |  ---------    ---------
  57.   |    | .|--> |    | .|--  |    | .|--> |    | .|-->etc...
  58.   ---------    ---------    ---------    ---------
  59.                              ^temp
  60.  
  61.  
  62. 3)  Now, just destroy the memory used by the node pointed to by temp.
  63.  
  64.  
  65. > Similarly, I need to write a function that would
  66. > insert a new entry into the link list whereby, the
  67. > argument is a pointer to the list entry to be
  68. > inserted.
  69.  
  70. Insertion is a little trickier, but not too bad... I won't answer here,
  71. partly because I think I need more details, partly because what I've
  72. said above may be enough for you.  If not, what kind of insert are we
  73. looking at?  I guess you're probably speaking of an ordered list, but
  74. not sure...  Anyway, as I said above, drawing pictures makes a huge
  75. difference to me... it makes breaking the problem into steps much
  76. easier.  Also, it helps to visualize what's happening, since pointers
  77. can be somewhat abstract.
  78.  
  79. Hope this helps... and if not, post back, I'd be happy to try and
  80. re-explain, or discuss insertion.
  81.  
  82. Derek
  83. a228lave@cdf.toronto.edu
  84.